
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import task20.AddressBookBean;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author cjm
 */
public class AddressBook {
    private Element addressbook;
    private Element offcie;

    private Object getNodeValue() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static class DocumentBoilder {

        public DocumentBoilder() {
        }

        private Document newDocument() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    
    public boolean newCard(AddressBookBean newcard) throws TransformerException, IOException{
        Document doc = null;
        Element addressbook = null,card;
        Element name,mobile = null,office = null,home = null,qq = null,email = null,other = null;
        
        boolean flag=false;
         
       
        try{
            DocumentBoilderFactory f=DocumentBoilderFactory.newInstance();
            DocumentBoilder db=f.newDocumentBoilder();
            
            File file=new File("addressbook");
            if(!file.exists()){
                file.mkdir();
            }
            file=new File("addressbook/addressbook.xml");
            if(!file.exists()){
                flag=true;
                file.createNewFile();
                doc=db.newDocument();
                addressbook=doc.createElement("addressbook");
            }
            
        }catch(Exception e){
            doc=db.parse(file);
            addressbook=(Element) doc.getElementsByTagName("addressbook").item(0);
            
        }
         card=doc.createElement("card");
         name=doc.createElement("name");
         mobile=doc.createElement("mobile");
         offcie=doc.createElement("offcie");
         home=doc.createElement("home");
         qq=doc.createElement("qq");
         email=doc.createElement("email");
         other=doc.createElement("other");
         
         name.appendChild(doc.createTextNode(newcard.getName()));
         mobile.appendChild(doc.createTextNode(newcard.getMobile()));
         office.appendChild(doc.createTextNode(newcard.getOffice()));
         home.appendChild(doc.createTextNode(newcard.getHome()));
         qq.appendChild(doc.createTextNode(newcard.getQq()));
         email.appendChild(doc.createTextNode(newcard.getEmail()));
         other.appendChild(doc.createTextNode(newcard.getOther()));
         
         card.appendChild(name);
         card.appendChild(mobile);
         card.appendChild(office);
         card.appendChild(home);
         card.appendChild(qq);
         card.appendChild(email);
         card.appendChild(other);
         
         addressbook.appendChild(card);
         if(flag){
             doc.appendChild(addressbook);
         }
         TransformerFactory ff=TransformerFactor.newInstance();
         Transformer t=ff.newTransformer();
         
         
         FileWriter fout=new FileWriter(file);
         t.transform(new DOMSource(doc), new StreamResult(fout));
         
        return true;
    }catch(Exception e){
    e.printStackTrace();
}
    
    
public boolean modifyCard(AddressBookBean oldcard) throws ParserConfigurationException{
    Document doc;
    NodeList cards;
    Element adderssbook,card,newcard,oldname;
    Element name,mobile,office,home,qq,email,other;
    try{
        DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=f.newDocumentBuilder();
        File file=new File("addressbook/addressbook.xml");
        if(!file.exists()){
            return false;
        }
        doc=db.newDocument();
        addressbook=(Element)doc.getElementsByTagName("addressbook").item(0);
        cards=doc.getElementsByTagName("card");
        
        for(int i=0;i<cards.getLength();i++){
            card=(Element)carsd.item(i);
            oldname=(Element)card.getElementsByTagName("name").item(0);
            if(oldname.getFirstChild()).getNodeValue().equals(oldcard.getName())){
            
         newcard=doc.createElement("card");
         name=doc.createElement("name");
         mobile=doc.createElement("mobile");
         offcie=doc.createElement("offcie");
         home=doc.createElement("home");
         qq=doc.createElement("qq");
         email=doc.createElement("email");
         other=doc.createElement("other");
            
          name.appendChild(doc.createTextNode(newcard.getName()));
         mobile.appendChild(doc.createTextNode(newcard.getMobile()));
         office.appendChilTextNode(newcard.getOffice()));
         home.appendChild(doc.createTextNode(newcard.getHome()));
         qq.appendChild(doc.createTextNode(newcard.getQq()));
         email.appendChild(doc.createTextNod(doc.createde(newcard.getEmail()));
         other.appendChild(doc.createTextNode(newcard.getOther()));
            
            
            
            
            
            
            
            
        }
            
                  

        
    }
    
    return true;
}

public boolean delCard(String name){
    return true;
}
public Hashtable getAllCards(){
    
    
    return null;
}

    public static void main(String[] args) {
        AddressBookBean newcard=new AddressBookBean("张三","1331111111","0822060500","156465487489","444444444444","qq@","no");
    }

}

